<?php
defined("_VALID_ACCESS") || die('Direct access forbidden');
class SCTV_MaintenanceSms extends Module{
 	public function body() {
		$theme = $this->pack_module('Base/Theme');
        $message = $_POST['message'];
		if(!empty($message))
        {
            $my_record = CRM_ContactsCommon::get_my_record();
            $message = CRM_ContactsCommon::contact_format_default($my_record, true) . ': ' . $message;

            $dbconn=pg_connect("host=localhost port=5432 dbname=sms user=postgres password=admin");
            foreach($_POST['companies'] as $key=>$contacts)
            {
                foreach($contacts['contacts'] as $key=>$value)
                {
                    $contact = Utils_RecordBrowserCommon::get_record('contact', $key);
                    if(!empty($contact) && !empty($contact['mobifone'])){
                        if($key == 119){
                            $message = mb_convert_encoding($message, 'US-ASCII','UTF-8');
                            //$message = htmlentities($message, 'ENT_COMPAT','UTF-8');
                        }
                        SCTV_MaintenanceSmsCommon::send_out($dbconn, $contact['mobifone'], $message, 'MANUAL', null, $contact['last_name'] . ' ' . $contact['first_name']);
                    }
                }
            }
            pg_close($dbconn);
        }

        $form = $this->init_module('Libs/QuickForm', null, rand());
        $form->reset();
        $companies = Utils_RecordBrowserCommon::get_records('company', array(), array(), array(), array(), true);
		foreach($companies as $company){
			$contacts = Utils_RecordBrowserCommon::get_records('contact', array('company_name'=>$company['id'], '!mobifone'=>''), array(), array(), array(), true);
            if(!empty($contacts)){
                $e = $form->addElement('checkbox', 'companies[' . $company['id'] . ']', $company['company_name'], $company['company_name'],
                    array(
                        "onchange"=>"
                            var company = jQuery(this);
                            var li = company.parent().parent();
                            li.find('ul input').each(function(i, object){ object.checked = company.prop('checked')});
                        "
                    )
                );
                foreach($contacts as $contact){
                    $full_name = $contact['last_name'] . ' ' . $contact['first_name'] . ' - ' . $contact['mobifone'];
                    $e = $form->addElement('checkbox', 'companies[' . $company['id'] . '][contacts][' . $contact['id'] . ']', $full_name, $full_name);
                }
            }
        }
        $form->addElement('textarea', 'message', 'Message', array('style'=>'height:100px;font-size:16px', 'maxlength'=>"160"));
        $form->addElement('submit', 'submit', __('Send'), array('style'=>'height:30px;width:60px;'));

        $form->assign_theme('form', $theme);

		ob_start();
		$rb = $this->init_module('Utils/RecordBrowser','SCTV_MaintenanceSms');
        $rb->set_header_properties(array(
            'content'=>array('width'=>40),
            'receivers'=>array('width'=>40),
            'Date'=>array('width'=>10),
            'Time'=>array('width'=>10)
        ));
        $rb->disable_pdf();
        $rb->disable_export();
        $rb->disable_actions();
        //$rb->set_defaults(array('requirement'=>$arg['id']));
        $rpm = array(array(), array(), array('date'=>'desc', 'time'=>'desc'));
        $this->display_module($rb,$rpm,'show_data');
        $table = ob_get_contents();
        ob_end_clean();
        $theme->assign('history', $table);
        $theme->display('default');
    }

    public function maintenance_addon($arg){
        $rb = $this->init_module('Utils/RecordBrowser','SCTV_MaintenanceRequirementSms');
        $rb->set_header_properties(array(
            'in_out'=>array('width'=>5),
            'employee'=>array('width'=>15),
            'phone_number'=>array('width'=>6),
            'content'=>array('width'=>50),
            'Date'=>array('width'=>12),
            'Time'=>array('width'=>12)
        ));
        $rb->set_defaults(array('maintenance'=>$arg['id']));
        $rpm = array(array('maintenance'=>$arg['id']), array(), array('date'=>'desc', 'time'=>'desc'));
        $this->display_module($rb,$rpm,'show_data');
    }
}
?>